<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <!-- 样 式 文 件 -->
    <link rel="stylesheet" href="/static/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/static/admin/admin/css/other/login.css"/>
</head>
<!-- 代 码 结 构 -->
<body background="/static/admin/admin/images/background.svg"
      style="background-size: cover;">
<form class="layui-form" action="javascript:void(0);">
    <div class="layui-form-item">
        <img class="logo" src="/static/admin/admin/images/logo.png"/>
        <div class="title">Pear Admin</div>
        <div class="desc">
            明 湖 区 最 具 影 响 力 的 设 计 规 范 之 一
        </div>
    </div>
    <div class="layui-form-item">
        <input name="username" type="text" placeholder="账 户 : admin " value="admin" hover class="layui-input"/>
    </div>
    <div class="layui-form-item">
        <input name="password" type="password" placeholder="密 码 : 123456 " value="123456" hover class="layui-input"/>
    </div>
    <div class="layui-form-item">
        <input placeholder="验证码 : " name="captcha" hover class="code layui-input layui-input-inline"/>
        <img src="/passport/getCaptcha" class="codeImage" id="captchaImage"/>
    </div>
    <div class="layui-form-item">
        <input type="checkbox" name="remember-me" title="记住密码" lay-skin="primary">
    </div>
    <div class="layui-form-item">
        <button class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
            登 入
        </button>
    </div>
</form>

<script src="/static/admin/component/layui/layui.js"></script>
<script src="/static/admin/component/pear/pear.js"></script>
<script>
  layui.use(['form', 'jquery', 'layer', 'button', 'popup'], function() {
    let form = layui.form;
    let $ = layui.jquery;
    let layer = layui.layer;
    let button = layui.button;
    let popup = layui.popup;
    let captchaPath = '/passport/getCaptcha';

    form.on('submit(login)', function(data) {
      // data 是表单的数据
      let loader = layer.load();  // 加载按钮
      let btn = button.load({elem: '.login'});
      $.ajax({
        url: '/api/v1/passport/login',
        data: JSON.stringify(data.field),
        contentType: 'application/json',
        type: 'post',
        dataType: 'json',
        // 请求成功之后返回的数据
        success: function(result) {
          layer.close(loader);  // 关闭加载按钮
          btn.stop(function() {
            if (result.success) {
              popup.success(result.message, function() {
                location.href = '/admin';
              });
            } else {
              popup.failure(result.message, function() {
                document.getElementById('captchaImage').src = captchaPath + '?' + Math.random();
              });
            }
          });
        },
      });
      return false;
    });

    // 点击按钮更新验证码
    $('#captchaImage').click(function() {
      document.getElementById('captchaImage').src = captchaPath + '?' + Math.random();
    });

    // 定时器，定时更新验证码
    setInterval(function() {
      document.getElementById('captchaImage').src = captchaPath + '?' + Math.random();
    }, 30 * 1000);
  });
</script>
<script>
  if (window !== top) {
    top.location.href = location.href;
  }
</script>
</body>
</html>